//
// Created by sgyt on 2020/5/28.
//

#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>

void printHost(struct hostent *pHostent);

int main(int argc, char *argv[])
{
    struct hostent *host = NULL;
    //使用tcp查询
    sethostent(1);

    //获取主机信息
    host = gethostent();

    if(host == NULL)
    {
        perror("host is error\n");
        exit(EXIT_FAILURE);
    }

    //关闭通道
    endhostent();


    printHost(host);

    return 0;
}

void printHost(struct hostent *pHostent) {
    char **addr_list = NULL;
    char **addr_al = NULL;

    char addr_alisase[100];
    char addr_p[INET_ADDRSTRLEN];

    printf("hostname:%s\n",pHostent->h_name);

    if(pHostent->h_addrtype == AF_INET)
    {
        printf("address type: AF_INET\n");
    }else{
        printf("address NOT type: AF_INET\n");
    }

    //获取域名
    for(addr_al = pHostent->h_aliases;*addr_al;addr_al++)
    {
        printf("aliases: %s\n",*addr_al);
    }

    printf("address length: %d\n", pHostent->h_length);

    printf("%x\n", *(int *)(*(pHostent->h_addr_list)));
    for(addr_list = pHostent->h_addr_list; *addr_list; addr_list++)
    {
        printf("address: %s\n", inet_ntop(pHostent->h_addrtype, *addr_list, addr_p, INET_ADDRSTRLEN));
    }

}
